package com.m.ioc;

import com.m.ioc.component.UserComponent;
import com.m.ioc.config.UserConfig;
import com.m.ioc.config.UserInfo;
import com.m.ioc.controller.UController;
import com.m.ioc.controller.UserController;
import com.m.ioc.repository.UserRepository;
import com.m.ioc.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.M.ioc")
@SpringBootApplication
public class IocApplication {

    public static void main(String[] args) {
        //Spring上下文
        ApplicationContext applicationContext = SpringApplication.run(IocApplication.class, args);

        //从context中获取bean
//        UserController bean = applicationContext.getBean(UserController.class);
//        bean.doController();

//        UserService userService = applicationContext.getBean(UserService.class);
//        userService.doService();
//
//        //根据名称来获取bean
//        UserService userService2 = (UserService) applicationContext.getBean("userService");
//        userService2.doService();
//
//        //根据名称和类型获取bean
//        UserService userService3 = (UserService) applicationContext.getBean("userService", UserService.class);
//        userService3.doService();
//
//        //特殊情况：如果类名前两位都为大写, bean的名称为类名
//        UController uController = (UController) applicationContext.getBean("UController");
//        uController.doUController();
//
//        UserRepository userRepository = applicationContext.getBean(UserRepository.class);
//        userRepository.doRepository();
//
//        UserComponent userComponent = applicationContext.getBean(UserComponent.class);
//        userComponent.doComponent();
//
//        UserConfig userConfig = applicationContext.getBean(UserConfig.class);
//        userConfig.doConfig();
//        System.out.println("userConfig = " + userConfig);
//
//        UserConfig userConfig2 = applicationContext.getBean(UserConfig.class);
//        userConfig2.doConfig();
//        System.out.println("userConfig2 = " + userConfig2);
//        System.out.println("userConfig == userConfig2 = " + (userConfig == userConfig2));

//        //@Bean演示
//        UserInfo userInfo = (UserInfo) applicationContext.getBean("userInfo");
//        System.out.println(userInfo);
//
//        UserInfo userInfo2 = (UserInfo) applicationContext.getBean("userInfo2", UserInfo.class);
//        System.out.println(userInfo2);
    }
}
